﻿function playlistsToHTML(playlists, areFavorites) {
	var playlistsHTML = '';
	playlistsHTML += '<table><tbody>';
	for (iPlaylist in playlists) {
		playlistsHTML += '<tr id="tr_playlist_' + playlists[iPlaylist].id + '"><td>' + playlists[iPlaylist].name + '</td>';
		playlistsHTML += '<td> <button type="button" onclick="listenPlaylist(' + playlists[iPlaylist].id + ')">Listen</button> </td>';
		playlistsHTML += '<td> <button type="button" onclick="viewPlaylist(' + playlists[iPlaylist].id + ')">View</button> </td>';
		if (areFavorites) {
			playlistsHTML += '<td> <button type="button" onclick="deleteFavorite(' + playlists[iPlaylist].id + ')">Delete</button> </td>';
		}
		else {
			playlistsHTML += '<td> <button type="button" onclick="showEditPlaylistMenu(' + playlists[iPlaylist].id + ')">Edit</button> </td>';
			playlistsHTML += '<td> <button type="button" onclick="deletePlaylist(' + playlists[iPlaylist].id + ')">Delete</button> </td>';
		}
		playlistsHTML += '</tr>';
	}
	playlistsHTML += '</tbody></table>';
	if (!areFavorites)
		playlistsHTML += '<button type="button" onclick="showCreatePlaylistDiv()">New Playlist</button>';
		
	return playlistsHTML;
}

function playlistToHTML(playlist) {
	var playlistHTML = '';
	
	playlistHTML += 'Title: ' + playlist.name;
	playlistHTML += '<table border="1"><tbody>';
	for (iSong in playlist.songs) {
		playlistHTML += '<tr><td>' + playlist.songs[iSong].title + '</td>';
		playlistHTML += '</tr>';
	}
	playlistHTML += '</tbody></table>';
	
	playlistHTML += 'Share this playlist with your friends: ';
	playlistHTML += '<input type="text" value="localhost/index.php?playlist=' + playlist.id + '"/>';
	playlistHTML += '<br/>';
	playlistHTML += '<td> <div id="positiveVotes_div">Positivos JS: ' + playlist.positiveVotes + ' </div> </td>';
	playlistHTML += '<td> <div id="negativeVotes_div">Negativos JS: ' + playlist.negativeVotes + ' </div> </td>';
	playlistHTML += '<td> <div id="votePositive_div"><button type="button" onclick="positiveVote(' + playlist.id + ')">+</button> </div></td>';
	playlistHTML += '<td> <div id="voteNegative_div"><button type="button" onclick="negativeVote(' + playlist.id + ')">-</button> </div></td>';

	playlistHTML += '<div id="favorite_button_div"></div>';
	
	return playlistHTML;
}

function showVotationButtons(show, playlistId) {
	if (show == "true") {
		$("#votePositive_div").html('<button type="button" onclick="positiveVote(' + playlistId + ')">+</button>');
		$("#voteNegative_div").html('<button type="button" onclick="negativeVote(' + playlistId + ')">-</button>');
	}
	else {
		$("#votePositive_div").html('');
		$("#voteNegative_div").html('');
	}
}

function showFavoriteButton(playlistId) {
	$("#favorite_button_div").html('<button type="button" onclick="addFavorite(' + playlistId + ')">Add to favourites</button>');
}

function showCreatePlaylistDiv() {
	$("#explorer_div").html(getEditPlaylistHTML());
	
	var songList = new SongList("playlist_table", "preview_div");
	// Se añade una canción vacía para comodidad del usuario
	songList.addSong("", "");
	
	$("#add_song_button").click(function() {
		songList.addSong();
	});
	$("#save_playlist_button").click(function() {
		// Construye la playlist mediante la información de la tabla
		var name = $("#playlist_name").val();
		var songs = new Array();
		$("#playlist_table tr").each(function() {
		if ($(this).find(".song_info > input").val() != "") {
			songs.push($(this).find(".song_info > input").val());
			songs.push($(this).find(".url_info > input").val());
		}
	});
	if (name != "" && songs.length > 0)
		createPlaylist(name, songs);
	});
}

function showEditPlaylistDiv(playlist) {
	$("#explorer_div").html(getEditPlaylistHTML());
	
	var songList = new SongList("playlist_table", "preview_div");
	
	$("#playlist_name").val(playlist.name);
	// Se insertan las canciones que hubiese en la playlist, listas para ser editadas
	for (iSong in playlist.songs)
		songList.addSong(playlist.songs[iSong].title, playlist.songs[iSong].link);
	
	$("#add_song_button").click(function() {
		songList.addSong();
	});
	$("#save_playlist_button").click(function() {
		// Construye la playlist mediante la información de la tabla
		var name = $("#playlist_name").val();
		var songs = new Array();
		$("#playlist_table tr").each(function() {
			songs.push($(this).find(".song_info > input").val());
			songs.push($(this).find(".url_info > input").val());
		});
		editPlaylist(playlist.id, name, songs);
	});
}

function getEditPlaylistHTML() {
	var html = '';
	html += '<div align="center">';
	html += 'Name <input type="text" id="playlist_name" />';
	html +=	'<div class="tableDemo">';
	html += '<table id="playlist_table">';
	html += '<tbody></tbody>';
	html += '</table>';
	html += '</div>';
	html += '<div>';
	html += '<button type="button" id="add_song_button">+</button>';
	html += '<button type="button" id="save_playlist_button">Save</button>';
	html += '</div>';
	html += '<div id="preview_div"></div>';
	html += '</div>';
		
	return html;
}